home *** CD-ROM | disk | FTP | other *** search
/ Freelog 121 / FreelogMagazineJuilletAout2014-No121.iso / Outils / Adobe-Air / adobe-air_13.exe / [0] / setup.swf / scripts / mx / resources / ResourceManagerImpl.as < prev   
Text File  |  2014-03-27  |  17KB  |  521 lines

  1. package mx.resources
  2. {
  3.    import flash.events.Event;
  4.    import flash.events.EventDispatcher;
  5.    import flash.events.IEventDispatcher;
  6.    import flash.events.TimerEvent;
  7.    import flash.system.ApplicationDomain;
  8.    import flash.system.Capabilities;
  9.    import flash.system.SecurityDomain;
  10.    import flash.utils.Timer;
  11.    import mx.core.mx_internal;
  12.    import mx.events.ModuleEvent;
  13.    import mx.events.ResourceEvent;
  14.    import mx.modules.IModuleInfo;
  15.    import mx.modules.ModuleManager;
  16.    import mx.utils.StringUtil;
  17.    
  18.    use namespace mx_internal;
  19.    
  20.    public class ResourceManagerImpl extends EventDispatcher implements IResourceManager
  21.    {
  22.       
  23.       mx_internal static const VERSION:String = "3.0.0.0";
  24.       
  25.       private static var instance:IResourceManager;
  26.        
  27.       
  28.       private var resourceModules:Object;
  29.       
  30.       private var initializedForNonFrameworkApp:Boolean = false;
  31.       
  32.       private var localeMap:Object;
  33.       
  34.       private var _localeChain:Array;
  35.       
  36.       public function ResourceManagerImpl()
  37.       {
  38.          localeMap = {};
  39.          resourceModules = {};
  40.          super();
  41.       }
  42.       
  43.       public static function getInstance() : IResourceManager
  44.       {
  45.          if(!instance)
  46.          {
  47.             instance = new ResourceManagerImpl();
  48.          }
  49.          return instance;
  50.       }
  51.       
  52.       public function get localeChain() : Array
  53.       {
  54.          return _localeChain;
  55.       }
  56.       
  57.       public function set localeChain(param1:Array) : void
  58.       {
  59.          _localeChain = param1;
  60.          update();
  61.       }
  62.       
  63.       [Bindable("change")]
  64.       public function getStringArray(param1:String, param2:String, param3:String = null) : Array
  65.       {
  66.          var _loc4_:IResourceBundle;
  67.          if(!(_loc4_ = findBundle(param1,param2,param3)))
  68.          {
  69.             return null;
  70.          }
  71.          var _loc5_:* = _loc4_.content[param2];
  72.          var _loc6_:Array;
  73.          var _loc7_:int = (_loc6_ = String(_loc5_).split(",")).length;
  74.          var _loc8_:int = 0;
  75.          while(_loc8_ < _loc7_)
  76.          {
  77.             _loc6_[_loc8_] = StringUtil.trim(_loc6_[_loc8_]);
  78.             _loc8_++;
  79.          }
  80.          return _loc6_;
  81.       }
  82.       
  83.       mx_internal function installCompiledResourceBundle(param1:ApplicationDomain, param2:String, param3:String) : void
  84.       {
  85.          var _loc4_:String = null;
  86.          var _loc5_:String = param3;
  87.          var _loc6_:int;
  88.          if((_loc6_ = param3.indexOf(":")) != -1)
  89.          {
  90.             _loc4_ = param3.substring(0,_loc6_);
  91.             _loc5_ = param3.substring(_loc6_ + 1);
  92.          }
  93.          if(getResourceBundle(param2,param3))
  94.          {
  95.             return;
  96.          }
  97.          var _loc7_:* = param2 + "$" + _loc5_ + "_properties";
  98.          if(_loc4_ != null)
  99.          {
  100.             _loc7_ = _loc4_ + "." + _loc7_;
  101.          }
  102.          var _loc8_:Class = null;
  103.          if(param1.hasDefinition(_loc7_))
  104.          {
  105.             _loc8_ = Class(param1.getDefinition(_loc7_));
  106.          }
  107.          if(!_loc8_)
  108.          {
  109.             _loc7_ = param3;
  110.             if(param1.hasDefinition(_loc7_))
  111.             {
  112.                _loc8_ = Class(param1.getDefinition(_loc7_));
  113.             }
  114.          }
  115.          if(!_loc8_)
  116.          {
  117.             _loc7_ = param3 + "_properties";
  118.             if(param1.hasDefinition(_loc7_))
  119.             {
  120.                _loc8_ = Class(param1.getDefinition(_loc7_));
  121.             }
  122.          }
  123.          if(!_loc8_)
  124.          {
  125.             throw new Error("Could not find compiled resource bundle \'" + param3 + "\' for locale \'" + param2 + "\'.");
  126.          }
  127.          var _loc9_:ResourceBundle;
  128.          (_loc9_ = ResourceBundle(new _loc8_())).mx_internal::_locale = param2;
  129.          _loc9_.mx_internal::_bundleName = param3;
  130.          addResourceBundle(_loc9_);
  131.       }
  132.       
  133.       [Bindable("change")]
  134.       public function getString(param1:String, param2:String, param3:Array = null, param4:String = null) : String
  135.       {
  136.          var _loc5_:IResourceBundle;
  137.          if(!(_loc5_ = findBundle(param1,param2,param4)))
  138.          {
  139.             return null;
  140.          }
  141.          var _loc6_:String = String(_loc5_.content[param2]);
  142.          if(param3)
  143.          {
  144.             _loc6_ = StringUtil.substitute(_loc6_,param3);
  145.          }
  146.          return _loc6_;
  147.       }
  148.       
  149.       public function loadResourceModule(param1:String, param2:Boolean = true, param3:ApplicationDomain = null, param4:SecurityDomain = null) : IEventDispatcher
  150.       {
  151.          var moduleInfo:IModuleInfo = null;
  152.          var resourceEventDispatcher:ResourceEventDispatcher = null;
  153.          var timer:Timer = null;
  154.          var timerHandler:Function = null;
  155.          var url:String = param1;
  156.          var updateFlag:Boolean = param2;
  157.          var applicationDomain:ApplicationDomain = param3;
  158.          var securityDomain:SecurityDomain = param4;
  159.          moduleInfo = ModuleManager.getModule(url);
  160.          resourceEventDispatcher = new ResourceEventDispatcher(moduleInfo);
  161.          var readyHandler:Function = function(param1:ModuleEvent):void
  162.          {
  163.             var _loc2_:* = param1.module.factory.create();
  164.             resourceModules[param1.module.url].resourceModule = _loc2_;
  165.             if(updateFlag)
  166.             {
  167.                update();
  168.             }
  169.          };
  170.          moduleInfo.addEventListener(ModuleEvent.READY,readyHandler,false,0,true);
  171.          var errorHandler:Function = function(param1:ModuleEvent):void
  172.          {
  173.             var _loc3_:ResourceEvent = null;
  174.             var _loc2_:String = "Unable to load resource module from " + url;
  175.             if(resourceEventDispatcher.willTrigger(ResourceEvent.ERROR))
  176.             {
  177.                _loc3_ = new ResourceEvent(ResourceEvent.ERROR,param1.bubbles,param1.cancelable);
  178.                _loc3_.bytesLoaded = 0;
  179.                _loc3_.bytesTotal = 0;
  180.                _loc3_.errorText = _loc2_;
  181.                resourceEventDispatcher.dispatchEvent(_loc3_);
  182.                return;
  183.             }
  184.             throw new Error(_loc2_);
  185.          };
  186.          moduleInfo.addEventListener(ModuleEvent.ERROR,errorHandler,false,0,true);
  187.          resourceModules[url] = new ResourceModuleInfo(moduleInfo,readyHandler,errorHandler);
  188.          timer = new Timer(0);
  189.          timerHandler = function(param1:TimerEvent):void
  190.          {
  191.             timer.removeEventListener(TimerEvent.TIMER,timerHandler);
  192.             timer.stop();
  193.             moduleInfo.load(applicationDomain,securityDomain);
  194.          };
  195.          timer.addEventListener(TimerEvent.TIMER,timerHandler,false,0,true);
  196.          timer.start();
  197.          return resourceEventDispatcher;
  198.       }
  199.       
  200.       public function getLocales() : Array
  201.       {
  202.          var _loc2_:* = null;
  203.          var _loc1_:Array = [];
  204.          for(_loc2_ in localeMap)
  205.          {
  206.             _loc1_.push(_loc2_);
  207.          }
  208.          return _loc1_;
  209.       }
  210.       
  211.       public function removeResourceBundlesForLocale(param1:String) : void
  212.       {
  213.          delete localeMap[param1];
  214.       }
  215.       
  216.       public function getResourceBundle(param1:String, param2:String) : IResourceBundle
  217.       {
  218.          var _loc3_:Object = localeMap[param1];
  219.          if(!_loc3_)
  220.          {
  221.             return null;
  222.          }
  223.          return _loc3_[param2];
  224.       }
  225.       
  226.       private function dumpResourceModule(param1:*) : void
  227.       {
  228.          var _loc2_:ResourceBundle = null;
  229.          var _loc3_:* = null;
  230.          for each(_loc2_ in param1.resourceBundles)
  231.          {
  232.             trace(_loc2_.locale,_loc2_.bundleName);
  233.             for(_loc3_ in _loc2_.content)
  234.             {
  235.             }
  236.          }
  237.       }
  238.       
  239.       public function addResourceBundle(param1:IResourceBundle) : void
  240.       {
  241.          var _loc2_:String = param1.locale;
  242.          var _loc3_:String = param1.bundleName;
  243.          if(!localeMap[_loc2_])
  244.          {
  245.             localeMap[_loc2_] = {};
  246.          }
  247.          localeMap[_loc2_][_loc3_] = param1;
  248.       }
  249.       
  250.       [Bindable("change")]
  251.       public function getObject(param1:String, param2:String, param3:String = null) : *
  252.       {
  253.          var _loc4_:IResourceBundle;
  254.          if(!(_loc4_ = findBundle(param1,param2,param3)))
  255.          {
  256.             return undefined;
  257.          }
  258.          return _loc4_.content[param2];
  259.       }
  260.       
  261.       [Bindable("change")]
  262.       public function getInt(param1:String, param2:String, param3:String = null) : int
  263.       {
  264.          var _loc4_:IResourceBundle;
  265.          if(!(_loc4_ = findBundle(param1,param2,param3)))
  266.          {
  267.             return 0;
  268.          }
  269.          var _loc5_:* = _loc4_.content[param2];
  270.          return int(_loc5_);
  271.       }
  272.       
  273.       private function findBundle(param1:String, param2:String, param3:String) : IResourceBundle
  274.       {
  275.          supportNonFrameworkApps();
  276.          return param3 != null ? getResourceBundle(param3,param1) : findResourceBundleWithResource(param1,param2);
  277.       }
  278.       
  279.       private function supportNonFrameworkApps() : void
  280.       {
  281.          if(initializedForNonFrameworkApp)
  282.          {
  283.             return;
  284.          }
  285.          initializedForNonFrameworkApp = true;
  286.          if(getLocales().length > 0)
  287.          {
  288.             return;
  289.          }
  290.          var _loc1_:ApplicationDomain = ApplicationDomain.currentDomain;
  291.          if(!_loc1_.hasDefinition("_CompiledResourceBundleInfo"))
  292.          {
  293.             return;
  294.          }
  295.          var _loc2_:Class = Class(_loc1_.getDefinition("_CompiledResourceBundleInfo"));
  296.          var _loc3_:Array = _loc2_.compiledLocales;
  297.          var _loc4_:Array = _loc2_.compiledResourceBundleNames;
  298.          installCompiledResourceBundles(_loc1_,_loc3_,_loc4_);
  299.          localeChain = _loc3_;
  300.       }
  301.       
  302.       public function getBundleNamesForLocale(param1:String) : Array
  303.       {
  304.          var _loc3_:* = null;
  305.          var _loc2_:Array = [];
  306.          for(_loc3_ in localeMap[param1])
  307.          {
  308.             _loc2_.push(_loc3_);
  309.          }
  310.          return _loc2_;
  311.       }
  312.       
  313.       public function getPreferredLocaleChain() : Array
  314.       {
  315.          return LocaleSorter.sortLocalesByPreference(getLocales(),getSystemPreferredLocales(),null,true);
  316.       }
  317.       
  318.       [Bindable("change")]
  319.       public function getNumber(param1:String, param2:String, param3:String = null) : Number
  320.       {
  321.          var _loc4_:IResourceBundle;
  322.          if(!(_loc4_ = findBundle(param1,param2,param3)))
  323.          {
  324.             return NaN;
  325.          }
  326.          var _loc5_:* = _loc4_.content[param2];
  327.          return Number(_loc5_);
  328.       }
  329.       
  330.       public function update() : void
  331.       {
  332.          dispatchEvent(new Event(Event.CHANGE));
  333.       }
  334.       
  335.       [Bindable("change")]
  336.       public function getClass(param1:String, param2:String, param3:String = null) : Class
  337.       {
  338.          var _loc4_:IResourceBundle;
  339.          if(!(_loc4_ = findBundle(param1,param2,param3)))
  340.          {
  341.             return null;
  342.          }
  343.          var _loc5_:*;
  344.          return (_loc5_ = _loc4_.content[param2]) as Class;
  345.       }
  346.       
  347.       public function removeResourceBundle(param1:String, param2:String) : void
  348.       {
  349.          delete localeMap[param1][param2];
  350.          if(getBundleNamesForLocale(param1).length == 0)
  351.          {
  352.             delete localeMap[param1];
  353.          }
  354.       }
  355.       
  356.       public function initializeLocaleChain(param1:Array) : void
  357.       {
  358.          localeChain = LocaleSorter.sortLocalesByPreference(param1,getSystemPreferredLocales(),null,true);
  359.       }
  360.       
  361.       public function findResourceBundleWithResource(param1:String, param2:String) : IResourceBundle
  362.       {
  363.          var _loc5_:String = null;
  364.          var _loc6_:Object = null;
  365.          var _loc7_:ResourceBundle = null;
  366.          if(!_localeChain)
  367.          {
  368.             return null;
  369.          }
  370.          var _loc3_:int = _localeChain.length;
  371.          var _loc4_:int = 0;
  372.          while(_loc4_ < _loc3_)
  373.          {
  374.             _loc5_ = localeChain[_loc4_];
  375.             if(_loc6_ = localeMap[_loc5_])
  376.             {
  377.                if(_loc7_ = _loc6_[param1])
  378.                {
  379.                   if(param2 in _loc7_.content)
  380.                   {
  381.                      return _loc7_;
  382.                   }
  383.                }
  384.             }
  385.             _loc4_++;
  386.          }
  387.          return null;
  388.       }
  389.       
  390.       [Bindable("change")]
  391.       public function getUint(param1:String, param2:String, param3:String = null) : uint
  392.       {
  393.          var _loc4_:IResourceBundle;
  394.          if(!(_loc4_ = findBundle(param1,param2,param3)))
  395.          {
  396.             return 0;
  397.          }
  398.          var _loc5_:* = _loc4_.content[param2];
  399.          return uint(_loc5_);
  400.       }
  401.       
  402.       private function getSystemPreferredLocales() : Array
  403.       {
  404.          var _loc1_:Array = null;
  405.          if(Capabilities["languages"])
  406.          {
  407.             _loc1_ = Capabilities["languages"];
  408.          }
  409.          else
  410.          {
  411.             _loc1_ = [Capabilities.language];
  412.          }
  413.          return _loc1_;
  414.       }
  415.       
  416.       public function installCompiledResourceBundles(param1:ApplicationDomain, param2:Array, param3:Array) : void
  417.       {
  418.          var _loc7_:String = null;
  419.          var _loc8_:int = 0;
  420.          var _loc9_:String = null;
  421.          var _loc4_:int = !!param2 ? int(param2.length) : 0;
  422.          var _loc5_:int = !!param3 ? int(param3.length) : 0;
  423.          var _loc6_:int = 0;
  424.          while(_loc6_ < _loc4_)
  425.          {
  426.             _loc7_ = param2[_loc6_];
  427.             _loc8_ = 0;
  428.             while(_loc8_ < _loc5_)
  429.             {
  430.                _loc9_ = param3[_loc8_];
  431.                mx_internal::installCompiledResourceBundle(param1,_loc7_,_loc9_);
  432.                _loc8_++;
  433.             }
  434.             _loc6_++;
  435.          }
  436.       }
  437.       
  438.       [Bindable("change")]
  439.       public function getBoolean(param1:String, param2:String, param3:String = null) : Boolean
  440.       {
  441.          var _loc4_:IResourceBundle;
  442.          if(!(_loc4_ = findBundle(param1,param2,param3)))
  443.          {
  444.             return false;
  445.          }
  446.          var _loc5_:* = _loc4_.content[param2];
  447.          return String(_loc5_).toLowerCase() == "true";
  448.       }
  449.       
  450.       public function unloadResourceModule(param1:String, param2:Boolean = true) : void
  451.       {
  452.          throw new Error("unloadResourceModule() is not yet implemented.");
  453.       }
  454.    }
  455. }
  456.  
  457. import mx.modules.IModuleInfo;
  458. import mx.resources.IResourceModule;
  459.  
  460. class ResourceModuleInfo
  461. {
  462.     
  463.    
  464.    public var resourceModule:IResourceModule;
  465.    
  466.    public var errorHandler:Function;
  467.    
  468.    public var readyHandler:Function;
  469.    
  470.    public var moduleInfo:IModuleInfo;
  471.    
  472.    function ResourceModuleInfo(param1:IModuleInfo, param2:Function, param3:Function)
  473.    {
  474.       super();
  475.       this.moduleInfo = param1;
  476.       this.readyHandler = param2;
  477.       this.errorHandler = param3;
  478.    }
  479. }
  480.  
  481. import flash.events.EventDispatcher;
  482. import mx.events.ModuleEvent;
  483. import mx.events.ResourceEvent;
  484. import mx.modules.IModuleInfo;
  485.  
  486. class ResourceEventDispatcher extends EventDispatcher
  487. {
  488.     
  489.    
  490.    function ResourceEventDispatcher(param1:IModuleInfo)
  491.    {
  492.       super();
  493.       param1.addEventListener(ModuleEvent.ERROR,moduleInfo_errorHandler,false,0,true);
  494.       param1.addEventListener(ModuleEvent.PROGRESS,moduleInfo_progressHandler,false,0,true);
  495.       param1.addEventListener(ModuleEvent.READY,moduleInfo_readyHandler,false,0,true);
  496.    }
  497.    
  498.    private function moduleInfo_progressHandler(param1:ModuleEvent) : void
  499.    {
  500.       var _loc2_:ResourceEvent = new ResourceEvent(ResourceEvent.PROGRESS,param1.bubbles,param1.cancelable);
  501.       _loc2_.bytesLoaded = param1.bytesLoaded;
  502.       _loc2_.bytesTotal = param1.bytesTotal;
  503.       dispatchEvent(_loc2_);
  504.    }
  505.    
  506.    private function moduleInfo_readyHandler(param1:ModuleEvent) : void
  507.    {
  508.       var _loc2_:ResourceEvent = new ResourceEvent(ResourceEvent.COMPLETE);
  509.       dispatchEvent(_loc2_);
  510.    }
  511.    
  512.    private function moduleInfo_errorHandler(param1:ModuleEvent) : void
  513.    {
  514.       var _loc2_:ResourceEvent = new ResourceEvent(ResourceEvent.ERROR,param1.bubbles,param1.cancelable);
  515.       _loc2_.bytesLoaded = param1.bytesLoaded;
  516.       _loc2_.bytesTotal = param1.bytesTotal;
  517.       _loc2_.errorText = param1.errorText;
  518.       dispatchEvent(_loc2_);
  519.    }
  520. }
  521.